<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://api.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.css" rel="stylesheet">
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.js"></script>
    <script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.js"></script>
    <link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.css"
        type="text/css">
    <script src='https://lib.baomitu.com/jquery/3.6.3/jquery.min.js'></script>
    <style>
        html,
        body {
            overflow: hidden;
            margin: 0;
        }

        #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
        }

        #marker {
            background-image: url('https://img.gejiba.com/images/db6e7706e2c3dcae834e44b0e888c767.jpg');
            background-size: cover;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            cursor: pointer;
        }

        #clear {
            position: absolute;
            z-index: 100;
            right: 10px;
            top: 10px;
            height: 50px;
            width: 200px;
        }
    </style>
</head>

<body>
    <button id="clear">清除所有标记</button>
    <div id="map"></div>
    <script>
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdjaGFvY2hhbyIsImEiOiJjbGU1aDZ2eWUwMXp4M29udmFnNnNyZjBhIn0.2Kd0ZX06ReEdBnZ9XU4XUA';
        const map = new mapboxgl.Map({
            container: 'map', // container ID
            style: 'mapbox://styles/mapbox/streets-v12', // style URL
            center: [114.30, 30.50],
            zoom: 12,
            projection: 'globe'
        });
        var labels = []
        function addLabel(center) {
            let marker = new mapboxgl.Marker({
                draggable: true,
            }).setLngLat(center);
            labels.push(marker);
            marker.addTo(map);
        }
        map.on("click", (evt) => {
            // addLabel();
            var { lng, lat } = evt.lngLat;
            var center = [lng, lat]
            addLabel(center)
        });

        $("#clear").click(function () {
            labels.forEach(item => {
                item.remove()
            })
        })
    </script>
</body>

</html>